//
//  HJVisitorView.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/5.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit
import SnapKit

//protocol HJVisitorViewDelegate :NSObjectProtocol {
//    
//    func registerButtonDidSeleted()
//    func loginButtonDidSeleted()
//    
//}


class HJVisitorView: UIView {
    
    //weak var delegate:HJVisitorViewDelegate?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 供外界调用 设置不同控制器的访客视图
    func setVisitorInfo(imgName: String?, message: String?){
        
        if let img = imgName, msg = message {
            
            circleView.hidden = true
            iconView.image = UIImage(named: img)
            messageLabel.text = msg
        }else{
            
            startAnim()
        }
    }
    
    // MARK: - 执行动画
    private func startAnim() {
        //初始化动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画时间
        anim.duration = 20
        //重复次数
        anim.repeatCount = MAXFLOAT
        //旋转360
        anim.toValue = 2 * M_PI
        // 在切换 tabbar 或者将应用切换到后台, anim会被释放
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
        
    }
    
    // MARK: - 监听点击
    
//    @objc private func registerButtonClick() {
//        
//        delegate?.registerButtonDidSeleted()
//    }
//    
//    @objc private func loginButtonClick() {
//        
//        delegate?.loginButtonDidSeleted()
//        
//    }
    
    
    // MARK: - 懒加载控件
    
    
    //图标
    private lazy var iconView: UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_image_house")
        let imageView = UIImageView(image: image)
        return imageView
    }()
    
    //圆圈
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //label
    private lazy var messageLabel: UILabel = {
        
        var label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = .Center
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        return label
    }()
    
    //注册按钮
    lazy var registerButton:UIButton = {
        
        let button = UIButton()
        //button.addTarget(self, action: "registerButtonClick", forControlEvents: .TouchUpInside)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        return button
    }()
    
    //登录按钮
    lazy var loginButton:UIButton = {
        
        let button = UIButton()
       // button.addTarget(self, action: "loginButtonClick", forControlEvents: .TouchUpInside)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
    
    //阴影
    private lazy var maskIconView:UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        let imageView = UIImageView(image: image)
        return imageView
    }()


}

//添加视图和约束

extension HJVisitorView{
    
    private func setupUI() {
    
    backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        //添加视图
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //添加约束
        
        //iconView
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        //圆圈
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
            
        }
        //文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.centerX.equalTo(circleView.snp_centerX)
            
        }
        
        //注册
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
//            make.width.equalTo(100)
//            make.height.equalTo(35)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        //登录
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(registerButton.snp_top)
            make.size.equalTo(registerButton.snp_size)
        }
        
        //
        //阴影
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self.snp_width)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(registerButton.snp_bottom)
        }
        
        
      
    }
    func demo() {
        //iconView
        iconView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //circleView
        circleView.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //label
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //宽度
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 224))
        //距离上边
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 16))
        //注册按钮
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        //登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        //yy
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-0-[maskIconView]-0-|", options: [], metrics: nil, views: ["maskIconView":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskIconView]-(offset)-[registerButton]", options: [], metrics: ["offset":-35 ], views: ["maskIconView":maskIconView,"registerButton":registerButton]))
    }

    

}
